# -*- coding: utf-8 -*-
import scrapy

from TodayHistory.util.CommonUtils import *


class HistorySpider(scrapy.Spider):
    name = 'history'
    allowed_domains = ['www.todayonhistory.com']
    start_urls = ['http://www.todayonhistory.com/']

    def parse(self, response):
        today = time.strftime("%Y年%m月%d日", time.localtime())
        today2 = time.strftime("%Y.%m.%d", time.localtime())
        today3 = time.strftime("%m月%d日", time.localtime())
        url = response.url
        html = get_one_page(url)
        pattern = '[\s\S]*?<li class="circlel[\s\S]*?<div class="t">[\s\S]*?<span>([\s\S]*?)</span>[\s\S]*?<a [\s\S]*?>([\s\S]*?)</a>[\s\S]*?'
        count = 0
        list = ['历史上的今天('+today2+')\n', '历史上'+today3+'都发生了什么']
        for history in parse_one_page(html, pattern):
            date = history[0]
            content = history[1]
            list.append(date + '，' + content)
            count += 1
        write_to_file(list, 'documentations/历史上的今天('+today2+').txt')
        print('{}共有{}条记录'.format(today, count))
